﻿Imports Runa.UI.Controls
Imports Runa.UI.Effects
Imports Runa.Resources.ResourceManager
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.UI.Styles

Namespace Global.Runa.UI.Controls

    Public Class TextContainerBase

        Protected _ColorData As Color()
        Protected _Size As Vector2
        Protected _TextColor As Color
        Protected _TextTexture As Texture2DObject
        Protected RenderTarget As RenderTarget2D
        Protected game As Game

        Public Property Location() As Vector2
        Public Property TextStyle() As TextStyle

        Public ReadOnly [Text] As String
        Public ReadOnly TextEffect As WpfEffectBase
        Public ReadOnly Background As Texture2DObject
        Public ReadOnly Font As SpriteFont

        Public Sub New(ByVal g As Game, ByVal [Text] As String, ByVal Size As Vector2, ByVal style As TextStyle)
            Me.New(g, [Text], style.Font, Size, style.TextColor, style.TextEffect, style.Background)
            If (style Is Nothing) Then
                style = New TextStyle
            End If
            Me.TextStyle = style
        End Sub

        Public Sub New(ByVal g As Game, ByVal [Text] As String, ByVal Font As SpriteFont, ByVal Size As Vector2, Optional ByVal IsGenerate As Boolean = True)
            Me._TextColor = Color.White
            Me.Text = [Text]
            Me.Font = Font
            Me.game = g
            Me._Size = Size
            Me.RenderTarget = New RenderTarget2D(g.GraphicsDevice, CInt(Me._Size.X), CInt(Me._Size.Y), False, SurfaceFormat.Color, DepthFormat.None)
            If IsGenerate Then
                Me.GenerateTexture()
            End If
        End Sub

        Public Sub New(ByVal g As Game, ByVal [Text] As String, ByVal Font As SpriteFont, ByVal Size As Vector2, ByVal TextColor As Color, Optional ByVal TextEffect As WpfEffectBase = Nothing, Optional ByVal Background As Texture2DObject = Nothing)
            Me.New(g, [Text], Font, Size, False)
            Me.TextEffect = TextEffect
            Me._TextColor = TextColor
            Me.Background = Background
            Me.GenerateTexture()
        End Sub

        Protected Overridable Sub GenerateTexture()
            Me.game.GraphicsDevice.SetRenderTarget(Me.RenderTarget)
            Me.game.GraphicsDevice.Clear(Color.Transparent)
            If (Not Me.Font Is Nothing) Then
                Dim sp As New SpriteBatch(Me.game.GraphicsDevice)
                If (Not Me.TextEffect Is Nothing) Then
                    Me.TextEffect.UpdataParameters(New GameTime)
                    sp.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Me.TextEffect.Effect)
                Else
                    sp.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend)
                End If
                sp.DrawString(Me.Font, Me.Text, Vector2.Zero, Me._TextColor)
                sp.End()
            End If
            Me.game.GraphicsDevice.SetRenderTarget(Nothing)
            Me._TextTexture = New Texture2DObject() With
            {
                .Texture = Me.RenderTarget
            }
            Me._Size = Me._TextTexture.Size
        End Sub

        Protected Overridable Function GetColorData() As Color()
            Dim colors As Color() = Nothing
            If (Not Me._TextTexture Is Nothing) Then
                colors = Me._TextTexture.GetData(Of Color)()
            End If
            Return colors
        End Function

        Public Overridable ReadOnly Property ColorData As Color()
            Get
                If (Me._ColorData Is Nothing) Then
                    Me._ColorData = Me.GetColorData
                End If
                Return Me._ColorData
            End Get
        End Property

        Public ReadOnly Property RenderRect As Rectangle
            Get
                Return New Rectangle(CInt(Me.Location.X), CInt(Me.Location.Y), CInt(Me._Size.X), CInt(Me._Size.Y))
            End Get
        End Property

        Public Property Size As Vector2
            Get
                Return Me._Size
            End Get
            Protected Set(ByVal value As Vector2)
                Me._Size = value
            End Set
        End Property

        Public ReadOnly Property TextColor As Color
            Get
                Return Me._TextColor
            End Get
        End Property

        Public Property TextTexture As Texture2DObject
            Get
                If (Me._TextTexture Is Nothing) Then
                    Me.GenerateTexture()
                End If
                Return Me._TextTexture
            End Get
            Protected Set(ByVal value As Texture2DObject)
                Me._TextTexture = value
            End Set
        End Property

    End Class

End Namespace